import {Injectable} from '@angular/core';
import {StorageUtil} from '../../utils/storage.util';

/**
 * @author leihfei
 * @date 2019-05-12
 * @description loading业务
 */
@Injectable()
export class LoadingServer {
  /**
   * loading数组
   */
  public loading = [];

  private loginS = 'llllllfsjdlfs';

  /**
   * 验证是否成功登录
   */
  private isLogin = false;

  /**
   * 加载loading
   */
  load(): void {
    this.loading.push(true);
  }

  /**
   * 隐藏loading
   */
  hide(): void {
    if (this.loading.length) {
      this.loading.splice(0, 1);
    }
  }

  /**
   * 登录成功之后调用
   */
  login(): void {
    StorageUtil.setLocalStorage(this.loginS, true);
  }

  /**
   * @author leihfei
   * @date 2019-05-13
   * @description 退出登录
   */
  logout(): void {
    this.isLogin = false;
    StorageUtil.clearLocal();
  }

  /**
   * @author leihfei
   * @date 2019-05-13
   * @description 验证是否登录
   */
  youLogin(): boolean {
    return StorageUtil.getLocalStorage(this.loginS);
  }

  /**
   *  进行路由检测
   * @param route 路由地址
   */
  checkRoute(route: string): boolean {
    /*
     * 每一次从localStorage中取出菜单，然后进行菜单的url匹配即可
     * */
    const userInfo = StorageUtil.getLocalStorage('user');
    if (userInfo.userType === 1) {
      return true;
    }
    route = route.substring(0, route.lastIndexOf('?'));
    const menu = StorageUtil.getLocalStorage('menu');
    if (menu) {
      if (JSON.stringify(menu).includes(route)) {
        return true;
      }
    }
    return false;
  }
}
